{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4b90a557",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "23dae813",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_current_cluster_state(cluster_name):\n",
    "    print(f\"cluster:{cluster_name}\")\n",
    "    return  \"\"\"ERROR: Failed to pull image \"chaocai/docker/dsp:latest\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d9469fb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import json\n",
    "\n",
    "\n",
    "funcs = {\"get_current_cluster_state\": get_current_cluster_state}\n",
    "\n",
    "\n",
    "def run(input):\n",
    "    msg=[{\"role\":\"user\",\"content\":input}]\n",
    "    ret = run_conversation(msg)\n",
    "    return ret[\"content\"] \n",
    "\n",
    "def run_conversation(msg):\n",
    "    response = openai.ChatCompletion.create(\n",
    "        engine=deployment,\n",
    "        model=model,\n",
    "        messages=msg,\n",
    "        functions=[\n",
    "            {\n",
    "                \"name\": \"get_current_cluster_state\",\n",
    "                \"description\": \"Get the current state in a given cluster\",\n",
    "                \"parameters\": {\n",
    "                    \"type\": \"object\",\n",
    "                    \"properties\": {\n",
    "                        \"cluster_name\": {\n",
    "                            \"type\": \"string\",\n",
    "                            \"description\": \"the name of the cluster\",\n",
    "                        },\n",
    "                        \n",
    "                    },\n",
    "                    \"required\": [\"cluster_name\"],\n",
    "                },\n",
    "            }\n",
    "        ],\n",
    "        function_call=\"auto\",\n",
    "    )\n",
    "\n",
    "    message =  response[\"choices\"][0][\"message\"]\n",
    "    print(\"----- message ----\")\n",
    "    print(message)\n",
    "    print(\"----- message ----\")\n",
    "    # 如果不需要调用function，则直接返回结果\n",
    "    if not message.get(\"function_call\"):\n",
    "        return message\n",
    "    \n",
    "    # 获取调用的方法\n",
    "    function_name = message[\"function_call\"][\"name\"]\n",
    "    function_args = json.loads(message[\"function_call\"][\"arguments\"])\n",
    "    print(function_args)\n",
    "    res = funcs[function_name](function_args[\"cluster_name\"]) \n",
    "    message[\"content\"]=None\n",
    "    msg.append(message)\n",
    "    msg.append({\n",
    "                \"role\": \"function\",\n",
    "                \"name\": function_name,\n",
    "                \"content\": res,\n",
    "            })\n",
    "    return run_conversation(msg)\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b6856718",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----- message ----\n",
      "{\n",
      "  \"role\": \"assistant\",\n",
      "  \"function_call\": {\n",
      "    \"name\": \"get_current_cluster_state\",\n",
      "    \"arguments\": \"{\\n  \\\"cluster_name\\\": \\\"DSP\\\"\\n}\"\n",
      "  }\n",
      "}\n",
      "----- message ----\n",
      "{'cluster_name': 'DSP'}\n",
      "cluster:DSP\n",
      "----- message ----\n",
      "{\n",
      "  \"role\": \"assistant\",\n",
      "  \"content\": \"The current issue with the 'DSP' cluster seems to be related to Docker. Specifically, it is unable to pull the image \\\"chaocai/docker/dsp:latest\\\". \\n\\nThis issue could be caused by several reasons:\\n\\n1. There might be network issues preventing pulling the Docker image.\\n2. The image might not exist or it's not accessible. Please validate whether the image \\\"chaocai/docker/dsp:latest\\\" is actually present and accessible.\\n3. Docker daemon might be down or not working properly.\\n\\nFollowing are some suggestions to tackle this issue:\\n\\n1. Check your network connection and try again.\\n2. Check if the Docker image \\\"chaocai/docker/dsp:latest\\\" exists and is accessible.\\n3. Check the Docker daemon, restart it if necessary.\\n4. If the Docker image is hosted on a private repository, please ensure correct authentication credentials are provided.\\n\\nPlease take the appropriate action and then check if the issue is resolved.\"\n",
      "}\n",
      "----- message ----\n",
      "The current issue with the 'DSP' cluster seems to be related to Docker. Specifically, it is unable to pull the image \"chaocai/docker/dsp:latest\". \n",
      "\n",
      "This issue could be caused by several reasons:\n",
      "\n",
      "1. There might be network issues preventing pulling the Docker image.\n",
      "2. The image might not exist or it's not accessible. Please validate whether the image \"chaocai/docker/dsp:latest\" is actually present and accessible.\n",
      "3. Docker daemon might be down or not working properly.\n",
      "\n",
      "Following are some suggestions to tackle this issue:\n",
      "\n",
      "1. Check your network connection and try again.\n",
      "2. Check if the Docker image \"chaocai/docker/dsp:latest\" exists and is accessible.\n",
      "3. Check the Docker daemon, restart it if necessary.\n",
      "4. If the Docker image is hosted on a private repository, please ensure correct authentication credentials are provided.\n",
      "\n",
      "Please take the appropriate action and then check if the issue is resolved.\n"
     ]
    }
   ],
   "source": [
    "#run(\"Hello\")\n",
    "print(run(\"What's wrong with the cluster 'DSP'? And if there's an error, give me some suggestion. \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b86826e9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
